package com.heg.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.heg.api.service.SmsModuleService;
import com.heg.api.util.PublicMethodsUtils;
import com.heg.api.util.SmsPlatformUtils;
import com.heg.bk.dao.MybatisBaseDao;
import com.heg.bk.entity.HegSms;

/**
 * 短信模块逻辑层业务（实现层）
 * @author 邓绪招
 * @time 2016-04-23
 * @version 1.0.1
 */
@Service("smsModuleServiceImpl")
public class SmsModuleServiceImpl implements SmsModuleService  {
	
	@Resource
	private MybatisBaseDao mybatisDao;

	public Map<String, Object> sec_codes_obtainreg(String telnumber) {
		Integer smsStatus = 0;
		String sendReturncode = "";
		String sendReturnmsg = "";
		String sendReturntime = "";
		Map<String, Object> retMaps = new HashMap<String, Object>();
		//获取验证码
		String secCodes = PublicMethodsUtils.getBuildSecCodes();
		//发送验证码
		Map<String, Object> result = SmsPlatformUtils.send_Messages(telnumber, "1", new String []{secCodes, "1"});
		if("000000".equals(result.get("statusCode"))){
			smsStatus = 0;
			sendReturncode = result.get("statusCode").toString();
			sendReturnmsg = result.get("data").toString();
			sendReturntime = PublicMethodsUtils.getCurrTime();
		} else if("160040".equals(result.get("statusCode"))) {
			smsStatus = 1;
			sendReturncode = result.get("statusCode").toString();
			sendReturnmsg = result.get("statusMsg").toString();
			sendReturntime = PublicMethodsUtils.getCurrTime();
		}
		//数据添加入库
		HegSms hegSms = new HegSms();
		hegSms.setSendUid(0);
		hegSms.setSendTelnumber("111111111111");
		hegSms.setReceiveUid(0);
		hegSms.setReceiveTelnumber(telnumber);
		hegSms.setSmsType(0); ///验证码类型为0
		hegSms.setSmsState(smsStatus);
		hegSms.setContent(secCodes);
		hegSms.setCtime(PublicMethodsUtils.getCurrTime());
		hegSms.setTriggerType(1); //获取验证码为app端触发
		hegSms.setSendReturncode(sendReturncode);
		hegSms.setSendReturnmsg(sendReturnmsg);
		hegSms.setSendReturntime(sendReturntime);
		hegSms.setDescription("注册验证码发送");
		Integer smsId = mybatisDao.insert(hegSms, "hegSmsSpace.insert_sms_toObj");
		if(smsId > 0){
			retMaps.put("smsId", hegSms.getSmsId());
			retMaps.put("seccodes", secCodes);
		}
		return retMaps;
	}

	public Map<String, Object> get_sms_toSmsId(String smsId) {
		List resultObjs = (List) mybatisDao.selectList(smsId, "hegSmsSpace.verify_timeliness");
		if(resultObjs != null && resultObjs.size() > 0){
			return (Map<String, Object>) resultObjs.get(0);
		} else {
			return null;
		}
	}

}
